package p41_autoconfig;


import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ClassUtils;

import java.util.Map;

/**
 * 自定义条件注解，条件注解会使用Condition的实现类
 * 并根据matches的返回值，判断条件注解是否装配bean
 */
public class MyConditional implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnExists.class.getName());
        String className = attributes.get("className").toString();
        boolean present = ClassUtils.isPresent(className, null);
        boolean exists = (boolean)attributes.get("exists");

        return exists?present:!present;
    }
}
